package com.glowdayz.glowmee;

import java.util.ArrayList;

import com.glowdayz.common.ActivityManager;
import com.glowdayz.model.PouchProductCMD;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SearchProduct extends FragmentActivity implements OnClickListener {
	
	private ActivityManager am = ActivityManager.getInstance();
	
    EditText searchProduct = null;
    String searchTextValue = "";
	int pouchCount = 0;
	int[] productArr = null;
	LinearLayout ll_fragment = null;
	
	static ArrayList<PouchProductCMD> productList = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_product);
		
		am.addActivity(this);
		
		productList = new ArrayList<PouchProductCMD>();
		
		ll_fragment = (LinearLayout) findViewById(R.id.ll_fragment);
		ImageView backBt = (ImageView) findViewById(R.id.backBt);
		backBt.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        ImageView view = (ImageView) v;
                        view.getDrawable().setColorFilter(0x77000000,PorterDuff.Mode.SRC_ATOP);
                        view.invalidate();
                        break;
                    }
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL: {
                        ImageView view = (ImageView) v;
                        view.getDrawable().clearColorFilter();
                        view.invalidate();
                        break;
                    }
                }
                return false;
            }
        });
		backBt.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		ImageView okBt = (ImageView) findViewById(R.id.okBt);
		okBt.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        ImageView view = (ImageView) v;
                        view.getDrawable().setColorFilter(0x77000000,PorterDuff.Mode.SRC_ATOP);
                        view.invalidate();
                        break;
                    }
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL: {
                        ImageView view = (ImageView) v;
                        view.getDrawable().clearColorFilter();
                        view.invalidate();
                        break;
                    }
                }
                return false;
            }
        });
		okBt.setOnClickListener(this);
		
		TextView textTitle = (TextView) findViewById(R.id.textTitle);
		textTitle.setText("제품검색");
		
		Intent fromIntent = getIntent();
		pouchCount = fromIntent.getIntExtra("PARAM_pouchCount", 0);
		productArr = fromIntent.getIntArrayExtra("PARAM_productArr");
		
		searchProduct = (EditText) findViewById(R.id.searchProduct);
		
		TextWatcher watcher = new TextWatcher() {
		    @Override
		    public void afterTextChanged(Editable s) {
		         //텍스트 변경 후 발생할 이벤트를 작성.
		    }
		 
		    @Override
		    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		         //텍스트의 길이가 변경되었을 경우 발생할 이벤트를 작성.
		    }
		 
		    @Override
		    public void onTextChanged(CharSequence s, int start, int before, int count) {
		         //텍스트가 변경될때마다 발생할 이벤트를 작성.
		         if (searchProduct.isFocusable()) {
		        	 searchTextValue = searchProduct.getText().toString().trim();
	        		 fragmentReplace(searchTextValue);
		         }
		    }
		};
		 
		searchProduct.addTextChangedListener(watcher);
	}
	
	public void fragmentReplace(String productTitle) {
        if (productTitle.equals("")) {
        	ll_fragment.setVisibility(View.INVISIBLE);
        } else {
        	ll_fragment.setVisibility(View.VISIBLE);
        	Bundle bundle = new Bundle();
    		bundle.putString("PARAM_productTitle", productTitle);
    		bundle.putInt("PARAM_pouchCount", pouchCount);
    		bundle.putIntArray("PARAM_productArr", productArr);
            Fragment newFragment = new SearchProductFm();
            newFragment.setArguments(bundle);
            final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        	transaction.replace(R.id.ll_fragment, newFragment);
        	transaction.commit();
        }
        
    }

	@Override
	public void onClick(View v) {
		Intent intent = new Intent();
		intent.putExtra("PARAM_productList", productList);
		setResult(Activity.RESULT_OK, intent);
		finish();
	}
	
	/*@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == Activity.RESULT_OK) {
			String requestText = data.getStringExtra("PARAM_requestText");
			FileInfo fileInfo = (FileInfo) data.getExtras().getSerializable("PARAM_file");
			
			Intent intent = new Intent();
			intent.putExtra("PARAM_idProduct", 1);
			intent.putExtra("PARAM_image", "request");
			
			intent.putExtra("PARAM_requestText", requestText);
			intent.putExtra("PARAM_file", fileInfo);
			setResult(Activity.RESULT_OK, intent);
			finish();
		}
	}*/
	
    @Override
	protected void onDestroy() {
		super.onDestroy();
		am.removeActivity(this);
	}

}
